package com.amusement.threePoker.service
{
	import com.amusement.threePoker.control.GameHallControl;
	import com.amusement.threePoker.control.RoomControl;
	
	import flash.events.AsyncErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.net.NetConnection;
	import flash.net.Responder;
	
	import mx.controls.Alert;

	public class ThreePokerSyncService
	{
		NetConnection.defaultObjectEncoding=flash.net.ObjectEncoding.AMF3;
		
		private static var _instance:ThreePokerSyncService;
		private var conn:NetConnection;
		public function ThreePokerSyncService()
		{
			conn=new NetConnection();
			conn.client = this;
		}
		
		
		public static function get instance():ThreePokerSyncService
		{
			if(_instance == null){
				_instance = new ThreePokerSyncService();
			}
			return _instance;
		}

		public static function set instance(value:ThreePokerSyncService):void
		{
			_instance = value;
		}

		
		public function connServer(playerName:String):void
		{
			var str:String = "rtmp://192.168.0.122/threePoker";
			conn.addEventListener(NetStatusEvent.NET_STATUS, statusHandler); 
			conn.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			conn.connect(str, playerName);
		}
		
		
		public function enterRoom(playerName:String, roomNum:String):void{
			conn.call("enterRoom",new Responder(onResult, onStatus),playerName,roomNum);
		}
		
		private function onResult(obj:Object):void
		{
			// TODO Auto Generated method stub
		}
		
		private function onStatus(obj:Object):void
		{
			trace("status:" + obj);
		}
		
		//=======================================================
		public function enterGameI(obj:Object):void{
			GameHallControl.instance.init(obj as Array);
		}
		
		public function enterRoomI(obj:Object):void{
			RoomControl.instance.init(obj as Array);
		}
		
		public function serverExceptionI(obj:Object):void{
			Alert.show(obj.toString());
		}
		
		
		//=======================================================
		
		private function statusHandler(evt:NetStatusEvent):void
		{
			trace(evt.info.code);
		}
		
		public function asyncErrorHandler(e:AsyncErrorEvent):void
		{
			Alert.show("服务器同步发生异常！");
		}
	}
}